/*
 * Banners.java
 *
 * Created on 11 de mayo de 2007, 16:26
 */

package despachador.administracion;

import dal.CampaniaDAL;
import excepciones.AvwException;
import gestores.administracion.GestorBanners;
import java.io.*;
import java.net.*;
import java.util.ArrayList;

import javax.servlet.*;
import javax.servlet.http.*;
import negocio.BannersWeb;
import negocio.CampaniaWeb;

/**
 *
 * @author Administrador
 * @version
 */
public class Banners extends  HttpServlet {
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        String opcion ="";
        if(request.getParameter("opcion")!=null){
            opcion = request.getParameter("opcion");
        }
        String banCodigo ="";
        if(request.getParameter("banCodigo")!= null){
            banCodigo = request.getParameter("banCodigo");
        }
        try{
            if(opcion.equals("eliminar")){
                if (!banCodigo.equals("")){
                    GestorBanners.eliminarBanners(banCodigo);
                }
                armarPagina(request, response);
            }else if (opcion.equals("modificar")){
                String camCodigo ="";
                String banNombre =""; 
                String banUrl = "";
                if (!banCodigo.equals("")){
                   if(request.getParameter("banNombre")!= null){
                       banNombre = String.valueOf(request.getParameter("banNombre"));
                   }
                   if(request.getParameter("banUrl")!= null){
                       banUrl = (String)request.getParameter("banUrl");
                   }
                   if (request.getParameter("camCodigo")!= null){
                        camCodigo = request.getParameter("camCodigo");
                    }
                    GestorBanners.modificarBanners(camCodigo, banNombre, banUrl, banCodigo, new ArrayList<String>());
                }
                armarPagina(request, response);
            }else if (opcion.equals("crear")){
               String camCodigo ="";
               String banNombre =""; 
               String banUrl = "";

               if(request.getParameter("banNombre")!= null){
                   banNombre = String.valueOf(request.getParameter("banNombre"));
               }
               if(request.getParameter("banUrl")!= null){
                   banUrl = (String)request.getParameter("banUrl");
               }
               if (request.getParameter("camCodigo")!= null){
                    camCodigo = request.getParameter("camCodigo");
               }
               GestorBanners.insertarBanners(camCodigo, banNombre, banUrl, new ArrayList<String>());

               armarPagina(request, response);
            }else{
                armarPagina(request, response);
            }
            
        }catch (AvwException e) {
            request.setAttribute("error",
                        e.getMessage());
                dispatch(request, response, "/compositor/pagina/error.jsp");
        }
        
                
    }
    
    /**
     * dispatcher method
     */
    protected void dispatch(HttpServletRequest request,
            HttpServletResponse response, String page) 
            throws javax.servlet.ServletException, 
            java.io.IOException {
        RequestDispatcher dispatcher = 
            getServletContext(). 
            getRequestDispatcher(page);
        dispatcher.forward(request, response);
    }
    private void armarPagina(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        // Especificamos que pagina se coloca en cada parte del layout
        
        request.setAttribute("1", "/vistas/administracion/encabezado.jsp");
        request.setAttribute("2", "/vistas/administracion/menu.jsp");
        request.setAttribute("3", "/vistas/administracion/banners.jsp");
        request.setAttribute("4", "/vistas/administracion/pie.jsp");
        
        dispatch(request, response, "/compositor/administracion/trescuerpos.jsp");
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    
}
